package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.ContestCancelFollowBean;
import com.example.administrator.yuejian_2.bean.ContestDetailBean;
import com.example.administrator.yuejian_2.bean.ContestFollowBean;
import com.example.administrator.yuejian_2.bean.ContestIsFollowedBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.ContestService;
import com.example.administrator.yuejian_2.interservice.FollowService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CoDetailActivity extends AppCompatActivity {

    private ImageView imageView;
    private TextView conname;
    private TextView contime;
    private TextView conaddress;
    private TextView consize;
    private TextView conweb;
    private TextView conintro;
    private int cid;
    private int uid;
    private int sta;
    private Context context;
    private ImageButton backButton;
    private ImageButton collectButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_codetail);

        cid = getIntent().getIntExtra("con_id",-1);
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        uid = sharedPreferences.getInt("user_id",0);

        imageView = (ImageView) findViewById(R.id.contest_imageView);
        conname = (TextView) findViewById(R.id.contest_name);
        contime = (TextView) findViewById(R.id.contest_time);
        conaddress = (TextView) findViewById(R.id.contest_address);
        consize = (TextView) findViewById(R.id.contest_size);
        conweb = (TextView) findViewById(R.id.contest_web);
        conintro = (TextView) findViewById(R.id.contest_intro);

        getcontestdetail();

        backButton = (ImageButton) findViewById(R.id.contest_imageButton3);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        iscofollowed();
        collectButton = (ImageButton) findViewById(R.id.contest_imageButton4);
        collectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sta == 0){
                    cofollow();
                }else {
                    conofollow();
                }

            }
        });


    }

    private void getcontestdetail(){
        context = CoDetailActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ContestService service = retrofit.create(ContestService.class);
        Call<ContestDetailBean> call = service.getcontest(cid);
        call.enqueue(new Callback<ContestDetailBean>() {
            @Override
            public void onResponse(Call<ContestDetailBean> call, Response<ContestDetailBean> response) {
                if (response.body() != null){
                    String cname = response.body().getCon_title();
                    String ctime = response.body().getCon_release_time();
                    String caddress = response.body().getCon_address();
                    String csize = response.body().getCon_sum();
                    String cweb = response.body().getCon_website();
                    String cintro = response.body().getCon_intro();
                    String pic = response.body().getCon_pic();

                    conname.setText(cname);
                    contime.setText(ctime);
                    conaddress.setText(caddress);
                    consize.setText(csize);
                    conweb.setText(cweb);
                    conintro.setText(cintro);

                    if (!pic.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/contestphoto/"+pic)
                                .into(imageView);

                }
            }

            @Override
            public void onFailure(Call<ContestDetailBean> call, Throwable t) {

            }
        });
    }

    private void iscofollowed(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<ContestIsFollowedBean> call = service.iscofollow(uid,cid);
        call.enqueue(new Callback<ContestIsFollowedBean>() {
            @Override
            public void onResponse(Call<ContestIsFollowedBean> call, Response<ContestIsFollowedBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collected);
                    sta = 1;
                }else {
                    collectButton.setImageResource(R.drawable.ic_collect);
                    sta = 0;
                }
            }

            @Override
            public void onFailure(Call<ContestIsFollowedBean> call, Throwable t) {

            }
        });
    }

    private void cofollow(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<ContestFollowBean> call = service.cofollow(uid,cid);
        call.enqueue(new Callback<ContestFollowBean>() {
            @Override
            public void onResponse(Call<ContestFollowBean> call, Response<ContestFollowBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collected);
                    Toast.makeText(getApplicationContext(),"收藏成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<ContestFollowBean> call, Throwable t) {

            }
        });

    }

    private void conofollow(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<ContestCancelFollowBean> call = service.conofollow(uid,cid);
        call.enqueue(new Callback<ContestCancelFollowBean>() {
            @Override
            public void onResponse(Call<ContestCancelFollowBean> call, Response<ContestCancelFollowBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collect);
                    Toast.makeText(getApplicationContext(),"取消收藏成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<ContestCancelFollowBean> call, Throwable t) {

            }
        });

    }


}
